package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.dao.admin.DeviceDao;
import com.siemens.industrial.web.mvc.pojo.DataTableQuery;
import com.siemens.industrial.web.mvc.service.admin.DeviceService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/29.
 */
@Controller
public class DeviceController {

    @Resource
    DeviceService deviceService;

    @Resource
    DeviceDao DeviceDao;

    @Resource
    TenantService tenantService;

    @RequestMapping(value = "/admin/api/v1/device/gatewayState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getGateWayState(HttpServletRequest request) {
        DataTableQuery dt = new DataTableQuery(request);
        if (dt.isDataTableRequest()) {
            Map<String, Object> map = deviceService.getGetWayStatusByDataTable(request);
            return map;
        } else {
            return dt.buildResponse(0, 0, new ArrayList<>());
        }
    }

    @RequestMapping(value = "/admin/device/gatewayState", method = RequestMethod.GET)
    public String getGateWayStatePage() {

        return "admin/gatewayState";
    }

    @RequestMapping(value = "/admin/device/getAllHeaders", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getChartHeader() {
        Map<String, Object> map = deviceService.getAllHeaderData();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    @RequestMapping(value = "/admin/device/getAllTenantNames", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTenantNames() {
        List<Map<String, Object>> list = deviceService.getAllTenantNames();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    @RequestMapping(value = "/admin/device/getAllHeadersByTenantId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getChartHeaderById(String tenantId) {
        Map<String, Object> map = deviceService.getAllHeaderDataByTenantId(tenantId);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    @RequestMapping(value = "/admin/device/search", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> search(String tenantId,String model,String type,String group,String line) {
        List<Map<String, Object>> map = deviceService.search(tenantId,model,type,group,line);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    @RequestMapping(value = "/admin/device/getAlertData", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAlertData(String model,String type) {
        Map<String, Object> map = deviceService.getAdminAlertData(model,type);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }
}
